/**
 * 数据请求
 */
import http from '@ohos.net.http';
import { Constants } from '../Constants';
import { OneQuipDto } from '../dto/OneQuipDto';
import { WeatherDto } from '../dto/WeatherDto';
import { Log } from '../Log';
import { FetchUtil } from '../utils/FetchUtil';

const fetchService = new FetchUtil();

export const getOneQuip = async () => {
  try {
    const result = await fetchService.get<any>(`https://apis.tianapi.com/one/index?key=${Constants.API_TOKEN_KEY}&rand=0`);
    Log.info(`fetch get successeed. data: ${JSON.stringify(result)}`);
    const oneQuip: OneQuipDto = {
      oneid: result.oneid,
      word: result.word,
      wordfrom: result.wordfrom,
      imgurl: result.imgurl,
      imgauthor: result.imgauthor,
      date: result.date
    }
    return oneQuip;
  } catch (err) {
    Log.info(`fetch failed. Cause: ${JSON.stringify(err)}`);
  }
}

export const getWeather = async () => {
  try {
    const result = await fetchService.get<any>(`https://apis.tianapi.com/tianqi/index?key=${Constants.API_TOKEN_KEY}&city=101170101&type=1`);
    Log.info(`fetch get weather successeed. data: ${JSON.stringify(result)}`);
    const weather: WeatherDto = {
      date: result.date,
      week: result.week,
      weather: result.weather,
      real: result.real
    };
    return weather;
  } catch (err) {
    Log.info(`fetch get weather failed. Cause: ${JSON.stringify(err)}`);
  }
}